﻿//--------------------------------------------------------------------
// All Rights Reserved , Copyright (C) 2012 ,  TECH, Ltd. 
//--------------------------------------------------------------------

using System;
using System.Data;
using System.Reflection;
using System.ServiceModel;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
	using RDIFramework.Utilities;
	

	/// <summary>
	/// ItemsService
	/// 服务层
	/// 
	/// 修改记录
	/// 
	///		2012-03-02 版本：1.0 EricHu 建立。
	///		
	/// 版本：1.0
	///
	/// <author>
	///		<name>EricHu</name>
	///		<date>2012-03-02</date>
	/// </author> 
	/// </summary>
	[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
	public class ItemsService : System.MarshalByRefObject, IItemsService
	{
        private string serviceName = RDIFrameworkMessage.ItemsService;
        /// <summary>
        /// .NET快速开发整合框架（RDIFramework.NET）数据库连接
        /// </summary>
        private readonly string RDIFrameworkDbConection = SystemInfo.RDIFrameworkDbConection;

        #region public DataTable GetDT(UserInfo userInfo) 获取列表
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <returns>数据表</returns>
        public DataTable GetDT(UserInfo userInfo)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif
            
            DataTable dataTable = new DataTable(CiItemsTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    string[] names = { CiItemsTable.FieldEnabled,CiItemsTable.FieldDeleteMark};
                    object[] values = { 1,0};
                    // 若是系统管理员，那就返回全部数据
                    if (userInfo.IsAdministrator)
                    {
                        dataTable = itemsManager.GetDT(names,values);
                    }
                    else
                    {
                        // 按数据权限来过滤数据
                        PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                        string[] ids = permissionScopeManager.GetResourceScopeIds(userInfo.Id, CiItemsTable.TableName, "Resource.ManagePermission");
                        dataTable = itemsManager.GetDT(ids);
                    }
                    dataTable.TableName = CiItemsTable.TableName;
                    // 添加日志
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_GetDT, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        #region public DataTable GetDTByParent(UserInfo userInfo, string parentId) 按父节点获取列表
        /// <summary>
        /// 按父节点获取列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">目标表</param>
        /// <param name="parentId">父级主键</param>
        /// <returns>数据表</returns>
        public DataTable GetDTByParent(UserInfo userInfo, string parentId)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(CiItemsTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    dataTable = itemsManager.GetDTByParent(parentId);
                    dataTable.TableName = CiItemsTable.TableName;
                    // 添加日志
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_GetDTByParent, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="id">主键</param>
        /// <returns>实体</returns>
        public CiItemsEntity GetEntity(UserInfo userInfo, string id)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            CiItemsEntity itemsEntity = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    itemsEntity = itemsManager.GetEntity(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_GetEntity, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return itemsEntity;
        }

        #region public string Add(UserInfo userInfo, BaseItemsEntity itemsEntity, out string statusCode, out string statusMessage) 添加实体
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="itemsEntity">实体</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>数据表</returns>
        public string Add(UserInfo userInfo, CiItemsEntity itemsEntity, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            string returnValue = string.Empty;
            statusCode = string.Empty;
            statusMessage = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    itemsManager.Identity = true;
                    itemsManager.ReturnId = false;
                    // 调用方法，并且返回运行结果
                    returnValue = itemsManager.Add(itemsEntity, out statusCode);
                    statusMessage = itemsManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_Add, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        #region public int Update(UserInfo userInfo, BaseItemsEntity itemsEntity, out string statusCode, out string statusMessage) 更新实体
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="itemsEntity">实体</param>
        /// <param name="statusCode">返回状态码</param>
        /// <param name="statusMessage">返回状态信息</param>
        /// <returns>数据表</returns>
        public int Update(UserInfo userInfo, CiItemsEntity itemsEntity, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            statusCode = string.Empty;
            statusMessage = string.Empty;
            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    // 更新数据
                    returnValue = itemsManager.Update(itemsEntity, out statusCode);
                    statusMessage = itemsManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_Update, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion

        /// <summary>
        /// 创建数据表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">表名</param>
        /// <param name="statusCode">状态返回码</param>
        /// <param name="statusMessage">状态返回信息</param>
        public void CreateTable(UserInfo userInfo, string tableName, out string statusCode, out string statusMessage)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            statusCode = string.Empty;
            statusMessage = string.Empty;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo);
                    // 创建表结构
                    itemsManager.CreateTable(tableName, out statusCode);
                    statusMessage = itemsManager.GetStateMessage(statusCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_CreateTable, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif
        }

        #region public int Delete(UserInfo userInfo, string tableName, string id) 删除
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="tableName">目标表</param>
        /// <param name="id">主键</param>
        /// <returns>影响的行数</returns>
        public int Delete(UserInfo userInfo, string tableName, string id)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager itemsManager = new CiItemsManager(dbProvider, userInfo, tableName);
                    returnValue = itemsManager.Delete(id);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.ItemsService_Delete, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion	

		/// <summary>
        /// 批量删除标志
		/// </summary>
		/// <param name="userInfo">用户</param>
        /// <param name="ids">主键数组</param>
		/// <returns>受影响的行数</returns>
        public int SetDeleted(UserInfo userInfo, string[] ids)
		{
			int returnValue = 0;

            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    CiItemsManager manager = new CiItemsManager(dbProvider, userInfo);
                    returnValue = manager.SetDeleted(ids);
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

			return returnValue;
		}
	}
}
